using System;
using System.Collections.Generic;

namespace Server.Engines.Harvest
{

    public class HarvestBank
    {
        private int m_current;
        private int m_maximum;
        private HarvestVein m_vein;
        private TimeSpan m_minRespwawn;
        private TimeSpan m_maxRespwawn;
        private DateTime m_nextRespawn;

        public int Current
        {
            get
            {
                CheckRespawn();
                return m_current;
            }
        }

        public HarvestVein Vein
        {
            get
            {
                CheckRespawn();
                return m_vein;
            }
            set
            {
                m_vein = value;
            }
        }

        public void CheckRespawn()
        {
            if (m_current == m_maximum || m_nextRespawn > DateTime.Now)
                return;

            m_current = m_maximum;
        }

        public void Consume(int amount, Mobile from)
        {
            CheckRespawn();

            if (m_current == m_maximum)
            {
                double min = m_minRespwawn.TotalMinutes;
                double max = m_maxRespwawn.TotalMinutes;
                double rnd = Utility.RandomDouble();

                m_current = m_maximum - amount;

                double minutes = min + (rnd * (max - min));
                
                m_nextRespawn = DateTime.Now + TimeSpan.FromMinutes(minutes);
            }
            else
            {
                m_current -= amount;
            }

            if (m_current < 0)
                m_current = 0;
        }

        public HarvestBank(HarvestVein vein, int maximum, TimeSpan minRespawn, TimeSpan maxRespawn)
        {
            m_vein = vein;
            m_maximum = maximum;
            m_current = maximum;
            m_minRespwawn = minRespawn;
            m_maxRespwawn = maxRespawn;
        }
    }
}